/*
 * Copyright (c) 2011, Marc-Andre Ferland - madrang@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the The Warrent Team nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *     * This software is free for non-commercial use. You may not use this
 *       software, in whole or in part, in support of any commercial product
 *       without the express consent of the author.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE WARRENT TEAM BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Security.Principal;

namespace OsHelper
{
	public static class Platform
	{
		public enum OsType : uint
		{
			Unknown = 0,
			Windows,
			MacOS,
			Linux
		}
		public struct OsInfo
		{
			public readonly string PlatformName;
			public readonly OsType Type;
			
			public OsInfo(string MyPlatformName)
			{
				this.PlatformName = MyPlatformName;
				
				switch (this.PlatformName) {
				case "Win32S":
					this.Type = Platform.OsType.Windows;
					break;
				case "Win32Windows":
					this.Type = Platform.OsType.Windows;
					break;
				case "Win32NT":
					this.Type = Platform.OsType.Windows;
					break;
				case "WinCE":
					this.Type = Platform.OsType.Windows;
					break;
				case "Xbox":
					this.Type = Platform.OsType.Windows;
					break;
				case "Unix":
					this.Type = Platform.OsType.Linux;
					break;
				case "MacOSX":
					this.Type = Platform.OsType.MacOS;
					break;
				default:
					this.Type = Platform.OsType.Unknown;
					break;
				}
			}
		}
		
		private static OsInfo osInfo;
		public static OsInfo CurrentOs
		{
			get {
				if(osInfo.Type == Platform.OsType.Unknown)
					osInfo = new OsInfo(Environment.OSVersion.Platform.ToString());
				return osInfo;
			}
		}
		
		/// <summary>
		/// True if running on Linux operating system.
		/// </summary>
		public static bool IsLinux
        {
			get { return CurrentOs.Type == OsType.Linux; }
		}
		
		/// <summary>
		/// True if running on Windows operating system.
		/// </summary>
		public static bool IsWindows
        {
			 get { return CurrentOs.Type == OsType.Windows; }
		}
		
		/// <summary>
		/// True if running on Mac operating system.
		/// </summary>
		public static bool IsMacOS
        {
			get { return CurrentOs.Type == OsType.MacOS; }
		}
		
		private static int isMono;
		public static bool IsMono {
			get {
				if (isMono == 0)
					isMono = Type.GetType ("Mono.Runtime") != null ? 1 : -1;
				return isMono == 1;
			}
		}
		
		private static string monoVersion;
		public static string MonoVersion {
			get {
				if (monoVersion == null) {
					if (!IsMono)
						throw new InvalidOperationException ();
					MethodInfo mi = Type.GetType ("Mono.Runtime").GetMethod ("GetDisplayName", BindingFlags.NonPublic|BindingFlags.Static);
					if (mi != null)
						monoVersion = (string) mi.Invoke (null, null);
					else
						monoVersion = string.Empty;
				}
				return monoVersion;
			}
		}
		
		private static WindowsPrincipal principal;
		public static bool IsAdmin
		{
			get
			{
				if(principal == null)
					principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
				return principal.IsInRole(WindowsBuiltInRole.Administrator);
			}
        }
		
	}
}

